package com.jt.tenant.dialog;

import android.content.Context;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.CarInfoActivity;
import com.jt.tenant.activity.EquipInfoActivity;
import com.jt.tenant.activity.ScannerActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.util.UtilToast;

/**
 * Created by Administrator on 2016/4/5.
 */
public class EditDialog extends JTDialog implements View.OnClickListener{

    private EditText et_message;

    private TextView tv_cancel,tv_submit;

    private ImageView close;

    private boolean exit = false;

    private String type = "0";//0：填写车架号，1：填写推荐人

    public EditDialog(Context context) {

        super(context);

        setContentView(R.layout.dialog_edit);

        initView();

    }

    private void initView() {

        (tv_cancel = (TextView) findViewById(R.id.id_cancel)).setOnClickListener(this);

        (tv_submit = (TextView) findViewById(R.id.id_confirm)).setOnClickListener(this);

        (close = (ImageView)findViewById(R.id.iv_dialog_close)).setOnClickListener(this);

        et_message = (EditText) findViewById(R.id.id_message);

    }

    public void setHint(String hint){

        et_message.setHint(hint);

    }

    public void setType(String type){

        this.type = type;

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.iv_dialog_close:

                this.dismiss();

                break;

            case R.id.id_cancel:

                switch (type) {

                    case "0":

                        this.dismiss();

                        break;

                    case "1":

                        if(exit) {


                            if (EquipInfoActivity.refreshDataListener != null) {

                                EquipInfoActivity.refreshDataListener.refreshData();

                            }

                            JTApplication.INSTANCE.finishActivity(ScannerActivity.class);

                            this.dismiss();

                        }else {

                            UtilToast.show(getContext(),"如取消填写将失去推荐资格，再次点击取消。");

                            exit = true;

                        }

                        break;

                }

                break;

            case R.id.id_confirm:

                switch (type) {

                    case "0":

                    if (et_message.getText().toString().trim().equals("")) {

                        UtilToast.show(getContext(), "请输入车架号");

                    } else if (et_message.getText().toString().trim().length() != 17) {

                        UtilToast.show(getContext(), "车架号为17位");

                    } else {

                        if (CarInfoActivity.refreshLinster != null) {

                            CarInfoActivity.refreshLinster.refresh(et_message.getText().toString().trim());

                            dismiss();

                        }

                    }

                        break;

                    case "1":

                        if(et_message.getText().toString().trim().equals("")){

                            UtilToast.show(getContext(),"请输入推荐人账号");

                        }else if(!Tools.isMobile(et_message.getText().toString().trim())){

                            UtilToast.show(getContext(),"请输入正确手机号");

                        }else if(et_message.getText().toString().trim().equals(JTApplication.jtPreferences.readPhone())){

                            UtilToast.show(getContext(),"不可填写本人账号");

                        }else {

                            if(ScannerActivity.refreshLinster != null){

                                ScannerActivity.refreshLinster.refresh(et_message.getText().toString().trim());

                                dismiss();

                            }

                        }

                        break;

                }

                break;

        }

    }
}
